package com.mwm.loyal.libs.mq;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

@Controller
public class MyController {

    @Autowired
    private JmsTemplate queueTemplate;
    @Autowired
    private Destination queueDestination;
    @Autowired
    private JmsTemplate topicTemplate;
    @Autowired
    private Destination topicDestination;

    @RequestMapping(value = "/send")
    @ResponseBody
    public String sendMsg(final String msg) {
        queueTemplate.send(queueDestination, new MessageCreator() {
            @Override
            public Message createMessage(Session arg0) throws JMSException {
                return arg0.createTextMessage(msg);
            }
        });
        return msg;
    }

    @RequestMapping(value = "/sendTopic")
    @ResponseBody
    public String sendTopicMsg(final String msg) {
        topicTemplate.send(topicDestination, new MessageCreator() {
            @Override
            public Message createMessage(Session arg0) throws JMSException {
                return arg0.createTextMessage(msg);
            }
        });
        return msg;
    }
}